package domain;

import com.mysql.jdbc.PreparedStatement;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class Product {
    private String id;

    // 构造方法

    private String name;
    private double price;
    private String category;
    private int pnum;
    private String imgurl;
    private String description;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public int getPnum() {
        return pnum;
    }

    public void setPnum(int pnum) {
        this.pnum = pnum;
    }

    public String getImgurl() {
        return imgurl;
    }

    public void setImgurl(String imgurl) {
        this.imgurl = imgurl;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    // 查询数据库表products里的所有商品记录，并返回一个产品List
    public List<Product> searchAll() {
        List<Product> ps = new ArrayList<Product>();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //加载数据库
            Class.forName("com.mysql.jdbc.Driver"); // cj.jcbc
            // 创建连接
            String url = "jdbc:mysql://localhost:3306/bookstore?useUnicode=true&characterEncoding=utf8";
            String username = "root";
            String password = "111111";
            conn = DriverManager.getConnection(url, username, password);
            // // 创建语句对象
            stmt = conn.createStatement();
            // 执行查询
            String sql = "SELECT * FROM products";
            rs = stmt.executeQuery(sql);
            // 处理结果集
            while (rs.next()) {
                Product p = new Product();
                p.setId(rs.getString("id"));
                p.setName(rs.getString("name"));
                p.setPrice(rs.getDouble("price"));
                p.setCategory(rs.getString("category"));
                p.setPnum(rs.getInt("pnum"));
                p.setImgurl(rs.getString("imgurl"));
                p.setDescription(rs.getString("description"));
                ps.add(p);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ps;
    }
    // 将产品信息添加到数据库products表
    public boolean add(Product product) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            //加载数据库
            Class.forName("com.mysql.jdbc.Driver"); // cj.jcbc
            // 创建连接
            String url = "jdbc:mysql://localhost:3306/bookstore?useUnicode=true&characterEncoding=utf8";
            String username = "root";
            String password = "111111";
            conn = DriverManager.getConnection(url, username, password);
            // // 创建语句对象
            String sql = "INSERT INTO products (id, name, price,category,pnum,imgurl,description) VALUES (?,?,?,?,?,?,?)";
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            //设置参数
            UUID productId = UUID.randomUUID();
            product.setId(productId.toString());
            product.setName(product.getName());
            product.setPrice(product.getPrice());
            product.setCategory(product.getCategory());
            product.setPnum(product.getPnum());
            product.setImgurl(product.getImgurl());
            product.setDescription(product.getDescription());
            // 设置参数
            pstmt.setString(1, product.getId());
            pstmt.setString(2, product.getName());
            pstmt.setDouble(3, product.getPrice());
            pstmt.setString(4, product.getCategory());
            pstmt.setInt(5, product.getPnum());
            pstmt.setString(6, product.getImgurl());
            pstmt.setString(7, product.getDescription());
           // 执行插入操作
            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (pstmt != null) pstmt.close();
                if (conn != null) conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }
    //在产品的javabean也即Product类中，添加search方法，接受传递的搜索参数，返回查询到的ps产品列表。
    public static List<Product> search(String searchfield) {
        // 创建产品列表
        List<Product> productList = new ArrayList<Product>();

        // 这里实现数据库查询逻辑
        // 假设使用JDBC进行数据库连接和查询
        try {
            // 要记得加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 创建数据库连接
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore?useUnicode=true&characterEncoding=utf8",
                    "root", "111111");
            // 预处理SQL语句，防止SQL注入
            String sql = "SELECT * FROM products WHERE name LIKE ?";
            PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1, "%" + searchfield + "%");

            // 执行查询
            ResultSet rs = pstmt.executeQuery();

            // 遍历结果集，创建Product对象并添加到产品列表中
            while (rs.next()) {
                Product product = new Product();
                product.setId(rs.getString("id"));
                product.setName(rs.getString("name"));
                product.setPrice(rs.getDouble("price"));
                product.setCategory(rs.getString("category"));
                product.setPnum(rs.getInt("pnum"));
                product.setImgurl(rs.getString("imgurl"));
                product.setDescription(rs.getString("description"));

                productList.add(product);
            }

            // 关闭资源
            rs.close();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        // 返回查询到的产品列表
        return productList;
    }
}


